package org.jgs1904.chanmufeng;

/**
 * 简单的懒汉式单例
 * 线程1执行完毕后，线程2再执行：
 *
 * @author: 默苍璃
 * @date: 2024-07-01 10:02
 */
public class LazySimpleSingleton1 {

    private static LazySimpleSingleton1 instance;

    private LazySimpleSingleton1() {
    }

    public static LazySimpleSingleton1 getInstance() {
        if (instance == null) {
            instance = new LazySimpleSingleton1();
        }
        return instance;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            LazySimpleSingleton1 singleton = LazySimpleSingleton1.getInstance();
            System.out.println("Thread 1: " + System.identityHashCode(singleton));
        });

        Thread thread2 = new Thread(() -> {
            LazySimpleSingleton1 singleton = LazySimpleSingleton1.getInstance();
            System.out.println("Thread 2: " + System.identityHashCode(singleton));
        });

        thread1.start();
        thread1.join(); // 等待线程1执行完毕

        thread2.start();
        thread2.join(); // 等待线程2执行完毕
    }

}
